﻿'
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2005
' by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'

Imports DotNetNuke
Imports DotNetNuke.Common
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Web.Mvp

Public Class EditZParentProjectNameZPresenter
    Inherits ModulePresenter(Of IEditZParentProjectNameZ, EditZParentProjectNameZModel)

#Region "Private Members"
    Private Controller As IZParentProjectNameZController
#End Region

#Region "Properties"
    Public ReadOnly Property ItemId() As Long
        Get
            Dim _ItemId As Integer = Null.NullInteger
            If Not String.IsNullOrEmpty(Request.Params("ItemId")) Then
                _ItemId = Int32.Parse(Request.Params("ItemId"))
            End If
            Return _ItemId
        End Get
    End Property
#End Region

#Region "Constructors"
    Public Sub New(ByVal _View As IEditZParentProjectNameZ)
        Me.New(_View, New ZParentProjectNameZController(New SqlDataProvider()))
    End Sub

    Public Sub New(ByVal _View As IEditZParentProjectNameZ, ByVal _Controller As IZParentProjectNameZController)
        MyBase.New(_View)
        If _Controller Is Nothing Then
            Throw New ArgumentNullException("_Controller", "_Controller is nothing.")
        End If
        Controller = _Controller

        ' add event handlers
        AddHandler View.Cancel, AddressOf Cancel
        AddHandler View.Delete, AddressOf Delete
        AddHandler View.Save, AddressOf Save
    End Sub
#End Region

#Region "Page Events"
    Protected Overrides Sub OnInit()
        MyBase.OnInit()
        If Not IsPostBack Then
            Try
                If Not Common.Utilities.Null.NullInteger = ItemId Then
                    ' get content
                    Dim objZParentProjectNameZ As ZParentProjectNameZInfo = Controller.GetZParentProjectNameZ(ModuleId, ItemId)
                    If Not objZParentProjectNameZ Is Nothing Then
                        View.Model.Item = objZParentProjectNameZ
                    Else ' security violation attempt to access item not related to this Module
                        Response.Redirect(NavigateURL(TabId))
                    End If
                Else
                    View.Model.Item = New ZParentProjectNameZInfo(ModuleId, UserId)
                    View.ShowDeleteButton(False)
                    View.ShowAuditControl(False)
                End If
            Catch exc As Exception
                'Show a UI message later
                LogException(exc)
            End Try
        End If
    End Sub

    Public Sub Cancel(ByVal sender As Object, ByVal e As EventArgs)
        Response.Redirect(NavigateURL(TabId))
    End Sub

    Public Sub Save(ByVal sender As Object, ByVal e As UIEventArgs(Of ZParentProjectNameZInfo))
        Try
            e.Item.CreatedByUser = UserId
            e.Item.ModuleId = ModuleId
            e.Item.ItemId = ItemId

            If Common.Utilities.Null.NullInteger = ItemId Then
                ' add the content within the ZParentProjectNameZ table
                e.Item.ItemId = Controller.AddZParentProjectNameZ(e.Item)
                If e.Item.ItemId = Null.NullInteger Then
                    Throw New Exception("Add Exception")
                End If
            Else
                ' update the content within the ZParentProjectNameZ table
                Controller.UpdateZParentProjectNameZ(e.Item)
            End If
            ' Redirect back to the portal home page
            Response.Redirect(NavigateURL(TabId))
        Catch exc As Exception    'Module failed to load
            'Show a UI message later
            LogException(exc)
        End Try
    End Sub

    Public Sub Delete(ByVal sender As Object, ByVal e As EventArgs)
        Try
            ' Only attempt to delete the item if it exists already
            Dim objZParentProjectNameZ As ZParentProjectNameZInfo = Controller.GetZParentProjectNameZ(ModuleId, ItemId)
            If Not objZParentProjectNameZ Is Nothing Then
                Controller.DeleteZParentProjectNameZ(ModuleId, ItemId)
                ' Redirect back to the portal home page
                Response.Redirect(NavigateURL(TabId))
            Else ' security violation attempt to access item not related to this Module
                Response.Redirect(NavigateURL(TabId))
            End If
        Catch exc As Exception
            'Show a UI message later
            LogException(exc)
        End Try
    End Sub
#End Region

End Class
